//
//  BHStadiumCoachCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/1.
//

import UIKit

class BHStadiumCoachCell: UITableViewCell {

    var coachDidSelectEntryDetailBlock:((Int,String) -> Void)?
    
    @IBOutlet weak var moreCoachBtn: UIButton!
    @IBOutlet weak var collectionView: UICollectionView!
    
    var coachArray: [BHCoacheModel]! {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
    
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.regiseterNib(nibName: "BHStadiumCoachItemCell")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        
    }
    
}

extension BHStadiumCoachCell: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.coachArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if self.coachArray.count == 0 {
            return .zero
        }
        return CGSize(width: 76, height: 155)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if self.coachArray.count == 0 {
            return .zero
        }
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if self.coachArray.count == 0 {
            return 0
        }
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
}
 
extension BHStadiumCoachCell: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHStadiumCoachItemCell", for: indexPath) as! BHStadiumCoachItemCell
        let model: BHCoacheModel = self.coachArray[indexPath.row]
        cell.coachImgView.sd_setImage(with: URL.init(string: model.imageUrl), placeholderImage: nil, options: .retryFailed, completed: nil)
        cell.coachName_label.text = model.name
        cell.introduce_label.isHidden = true
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model: BHCoacheModel = self.coachArray[indexPath.row]
        self.coachDidSelectEntryDetailBlock?(indexPath.row,model.coachId)
    }
}
